package com.excesys.exsecs.energy.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.energy.entity.DimSubitem;
import com.excesys.exsecs.energy.service.DimSubitemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 能耗维度：分项
 *
 * @author lxc
 * @date 2024-03-04 14:09:43
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dimSubitem" )
@Tag(description = "dimSubitem" , name = "能耗维度：分项" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DimSubitemController {

    private final DimSubitemService dimSubitemService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param dimSubitem 能耗维度：分项
无其他选项
分项按国标列4项：照明，空调，动力，特殊，这4个基本项不能删除修改，但可以增删改查子项
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('twinops_dimSubitem_view')" )
    public R getDimSubitemPage(@ParameterObject Page page, @ParameterObject DimSubitem dimSubitem) {
        LambdaQueryWrapper<DimSubitem> wrapper = Wrappers.lambdaQuery();
        return R.ok(dimSubitemService.page(page, wrapper));
    }

	/**
	 * 获取分项树
	 * @return
	 */
	@Operation(summary = "获取分项树" , description = "获取分项树" )
	@GetMapping("/tree" )
	@PreAuthorize("@pms.hasPermission('twinops_dimSubitem_view')" )
	public R getTree(@ParameterObject DimSubitem dimSubitem) {
		return R.ok(dimSubitemService.queryTree(dimSubitem));
	}


    /**
     * 通过id查询能耗维度：分项
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('twinops_dimSubitem_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(dimSubitemService.getSubitemById(id));
    }

    /**
     * 新增能耗维度：分项
     * @param dimSubitem 能耗维度：分项
     * @return R
     */
    @Operation(summary = "新增能耗维度：分项" , description = "新增能耗维度：分项" )
    @SysLog("新增能耗维度：分项" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('twinops_dimSubitem_add')" )
    public R save(@RequestBody DimSubitem dimSubitem) {
        return R.ok(dimSubitemService.save(dimSubitem));
    }

	/**
	 * 关联表计保存
	 * @return R
	 */
	@Operation(summary = "关联表计保存" , description = "关联表计保存" )
	@SysLog("关联表计保存" )
	@PostMapping("/saveDevSubitemMap")
	@PreAuthorize("@pms.hasPermission('twinops_dimSubitem_add')" )
	public R saveDevSubitemMap(@RequestBody DimSubitem dimSubitem) {
		return R.ok(dimSubitemService.saveDevSubitemMap(dimSubitem));
	}

	/**
	 * 通过parentId查询关联的表计信息
	 * @param dimSubitem dimSubitem
	 * @return R
	 */
	@Operation(summary = "通过parentId查询关联的表计信息" , description = "通过parentId查询关联的表计信息" )
	@GetMapping("/meter" )
	@PreAuthorize("@pms.hasPermission('twinops_dimSubitem_view')" )
	public R getMetersById(@ParameterObject DimSubitem dimSubitem) {
		return R.ok(dimSubitemService.getMetersById(dimSubitem));
	}

    /**
     * 修改能耗维度：分项
     * @param dimSubitem 能耗维度：分项
     * @return R
     */
    @Operation(summary = "修改能耗维度：分项" , description = "修改能耗维度：分项" )
    @SysLog("修改能耗维度：分项" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('twinops_dimSubitem_edit')" )
    public R updateById(@RequestBody DimSubitem dimSubitem) {
        return R.ok(dimSubitemService.updateById(dimSubitem));
    }

    /**
     * 通过id删除能耗维度：分项
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除能耗维度：分项" , description = "通过id删除能耗维度：分项" )
    @SysLog("通过id删除能耗维度：分项" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('twinops_dimSubitem_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(dimSubitemService.removeSubitemBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param dimSubitem 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('twinops_dimSubitem_export')" )
    public List<DimSubitem> export(DimSubitem dimSubitem,Long[] ids) {
        return dimSubitemService.list(Wrappers.lambdaQuery(dimSubitem).in(ArrayUtil.isNotEmpty(ids), DimSubitem::getId, ids));
    }
}